package com.bbcare.followup.plat.service.impl;

import com.bbcare.comm.ToolUtils;
import com.bbcare.followup.plat.entity.Patient;
import com.bbcare.followup.plat.entity.PhoneModifyHis;
import com.bbcare.followup.plat.entity.PtContent;
import com.bbcare.followup.plat.service.IPatientPhoneService;

import com.bbcare.followup.plat.store.ibatis.IPatientAccountDAO;
import com.bbcare.followup.plat.store.ibatis.IPatientDAO;
import com.bbcare.followup.plat.store.ibatis.IPatientPhoneDao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PatientPhoneService implements IPatientPhoneService {

    private final Log logger = LogFactory.getLog(PatientPhoneService.class);

    @Autowired
    private IPatientDAO patientDAO;

    @Autowired
    private IPatientPhoneDao patientPhoneDao;

    @Override
    @Transactional
    public void updatePatientPhone(String tenantId,String userId,String phone,String updateAuth) throws Exception  {


        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("tenantId",tenantId);
        //根据用户信息和租户信息查询手机号是否修改 pt_user_info
        Patient patient = patientDAO.selectById(params);
        List<String> phones = patientPhoneDao.selectPhoneByAccount(patient.getAccountId());
        if(phones.size()!=1){
            logger.error(patient.getAccountId()+"数据有误！"+phone+"手机号更新失败");
            return;
        }
        //未修改手机号直接返回
        if(phone.equals(phones.get(0))){
            logger.info("手机号未作修改，不处理");
            return;
        }


        PhoneModifyHis phoneModifyHis  = new PhoneModifyHis();
        phoneModifyHis.setUpdateDate(new Date());
        phoneModifyHis.setUpdateAuth(updateAuth);
        phoneModifyHis.setNewPhone(phone);
        phoneModifyHis.setOldPhone(phones.get(0));
        phoneModifyHis.setAccount(tenantId);
        phoneModifyHis.setType("修改");
        //第一步 查找该用的账户并且修改 pt_account_info
        params.clear();
        params.put("id",patient.getAccountId());
        params.put("newPhone",phone);
        patientPhoneDao.updatePatientAccountPhone(params);

        //第二步 修改该账户关联其他队列信息
        params.clear();
        params.put("accountId",patient.getAccountId());
        params.put("phoneNo",phones.get(0));
        params.put("newPhoneNo",phone);

        //获得该账户下所有需要修改的队列
        List<String> list = patientPhoneDao.selectPtUserInfo(params);
        phoneModifyHis.setUserIds(ToolUtils.listToString(list));
        //修改该账户下所有需要修改的队列
        patientPhoneDao.updatePtUserInfo(params);

        //查询所有要修改的建档信息
        params.put("userid",phoneModifyHis.getUserIds());
        List<String> list2 = patientPhoneDao.selectPtContentData(params);
        phoneModifyHis.setContentIds(ToolUtils.listToString(list2));
        params.put("newPhoneNo",phone);
        //修改建档记录
        patientPhoneDao.updatePtContentData(params);
        patientPhoneDao.insertPhoneModifyHis(phoneModifyHis);

    }
}
